home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / fax / src / util / TypeRules.h < prev    next >
C/C++ Source or Header  |  1994-08-01  |  7KB  |  178 lines

  1. /*    $Header: /usr/people/sam/fax/util/RCS/TypeRules.h,v 1.7 1994/02/28 14:24:01 sam Rel $ */
  2. /*
  3.  * Copyright (c) 1990, 1991, 1992, 1993, 1994 Sam Leffler
  4.  * Copyright (c) 1991, 1992, 1993, 1994 Silicon Graphics, Inc.
  5.  *
  6.  * Permission to use, copy, modify, distribute, and sell this software and 
  7.  * its documentation for any purpose is hereby granted without fee, provided
  8.  * that (i) the above copyright notices and this permission notice appear in
  9.  * all copies of the software and related documentation, and (ii) the names of
  10.  * Sam Leffler and Silicon Graphics may not be used in any advertising or
  11.  * publicity relating to the software without the specific, prior written
  12.  * permission of Sam Leffler and Silicon Graphics.
  13.  * 
  14.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
  15.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
  16.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
  17.  * 
  18.  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  19.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  20.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  21.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
  22.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
  23.  * OF THIS SOFTWARE.
  24.  */
  25. #ifndef _TYPERULES_
  26. #define    _TYPERULES_
  27. /*
  28.  * Fax Submission File Type and Conversion Rules.
  29.  */
  30. #include "Types.h"
  31. #include "Str.h"
  32. #include "Obj.h"
  33.  
  34. /*
  35.  * A type rule database is an ASCII file that contains
  36.  * type deduction and conversion rules.  The format is
  37.  * as follows:
  38.  *
  39.  * FlexFAX file type rules.
  40.  *
  41.  * This file contains the file typing rules used by the sendfax
  42.  * program to deduce how input files should be prepared for fax
  43.  * transmission.  The format of this file is based on the System
  44.  * V /etc/magic file used by the file(1) program.  The code that
  45.  * reads this file was written entirely based on the comments that
  46.  * exist at the top of the magic file and describe how it works.
  47.  * The use of magic-style rules is intended to make it easier for
  48.  * users to reuse rules already designed for use with file(1).
  49.  *
  50.  * The fields on each line are:
  51.  *
  52.  * offset: a byte offset in the file at which data should be extracted
  53.  *      and compared to a matching string or value.  If this value
  54.  *      begins with '>', then an additional rule is used and scanning
  55.  *      continues to the next type rule line that does not begin with
  56.  *      a '>'.
  57.  * datatype: the type of data value to extract the specified offset in the
  58.  *      for comparison purposes.  This can be byte, short, long, or
  59.  *      string (a not necessarily null-terminated string of bytes).
  60.  *      A byte is 8 bits, short 16 bits, and long 32 bits.
  61.  * match:  the value and operation to use in matching; the value used is
  62.  *      based on the datatype field.  This value may be "x" to mean
  63.  *      "match anything".  The operation is "=" if nothing is specified;
  64.  *      otherwise it can be one of ">", "<", "<=", ">=", "!=", "&"
  65.  *      (for checking if a set of bits is on), "^" (for xor-ing and
  66.  *      comparing to zero), and "!" (for checking if a set of bits is off).
  67.  * result: one of "PS", "TIFF, or "error" (case insensitive).  The first
  68.  *      two results specifiy whether the rule generates a PostScript
  69.  *      file or a bilevel TIFF image.  The "error" result indicates a
  70.  *      file is unsuitable for transmission and if supplied as an
  71.  *      argument to sendfax, the command should be aborted.
  72.  * rule:  a string passed to the shell to convert the input file
  73.  *      to the result format (suitable for sending as facsimile).
  74.  *      The rule string is a printf-like string that should use the
  75.  *      following "%" escapes:
  76.  *        %i    input file name
  77.  *        %o    output file name
  78.  *        %r    output horizontal resolution in pixels/mm
  79.  *        %R    output horizontal resolution in pixels/inch
  80.  *        %v    output vertical resolution in lines/mm
  81.  *        %V    output vertical resolution in lines/inch
  82.  *        %f    data format, 1 for 1-d encoding or 2 for 2-d encoding
  83.  *        %w    page width in mm
  84.  *        %W    page width in pixels
  85.  *        %l    page length in mm
  86.  *        %L    page length in inches
  87.  *        %s    page size by name
  88.  *        %<x>    the <x> character (e.g. ``%%'' results in ``%''
  89.  */
  90. class TypeRule;
  91. class TypeRuleArray;
  92.  
  93. class TypeRules {
  94. public:
  95.     TypeRules();
  96.     ~TypeRules();
  97.  
  98.     static TypeRules* read(const fxStr& file);    // read rule database
  99.  
  100.     void setVerbose(fxBool);
  101.  
  102.     const TypeRule* match(const void* data, u_int size) const;
  103. private:
  104.     TypeRuleArray* rules;
  105.     fxBool    verbose;            // while matching
  106.  
  107.     u_int match2(u_int base, const void* data, u_int size, fxBool verb) const;
  108. };
  109.  
  110. typedef unsigned int TypeResult;        // conversion result
  111.  
  112. /*
  113.  * Type rules specify how to convert a file that is
  114.  * submitted for transmission into a format suitable
  115.  * for the fax server.  File types are based on an
  116.  * analysis of the file's ``magic number''.  Type
  117.  * conversions are specified by a parameterized string
  118.  * of commands to pass to a shell.
  119.  */
  120. class TypeRule : public fxObj {
  121. public:
  122.     enum {
  123.     TIFF,        // bilevel Group 3-encoded TIFF
  124.     POSTSCRIPT,    // PostScript
  125.     ERROR        // recognized erronious format
  126.     };
  127. private:
  128.     off_t    off;    // byte offset in file
  129.     fxBool    cont;    // continuation
  130.     enum {
  131.     ASCII,        // ascii-only string
  132.     STRING,        // byte string
  133.     ADDR,        // address of match
  134.     BYTE,        // 8 bits
  135.     SHORT,        // 16 bits
  136.     LONG,        // 32 bits
  137.     } type;        // data value type
  138.     enum {
  139.     ANY,        // match anything
  140.     EQ,        // == value
  141.     NE,        // != value
  142.     LT,        // < value
  143.     LE,        // <= value
  144.     GT,        // > value
  145.     GE,        // >= value
  146.     AND,        // (&value) != 0
  147.     XOR,        // (^value) != 0
  148.     NOT        // (!value) != 0
  149.     } op;        // match operation
  150.     union {
  151.     long  v;
  152.     char* s;
  153.     } value;        // matching value
  154.     TypeResult    result;    // result of applying rule
  155.     fxStr    cmd;    // shell command/error message
  156.  
  157.     friend TypeRules* TypeRules::read(const fxStr& file);
  158. public:
  159.     TypeRule();
  160.     virtual ~TypeRule();
  161.  
  162.     fxBool    match(const void*, u_int size, fxBool verbose = FALSE) const;
  163.     fxBool    isContinuation() const;
  164.  
  165.     TypeResult    getResult() const;
  166.     const fxStr& getCmd() const;
  167.     fxStr getErrMsg() const;
  168.     fxStr getFmtdCmd(const fxStr& input, const fxStr& output,
  169.             float hr, float vr,
  170.             const fxStr& df,
  171.             const fxStr& pname) const;
  172. };
  173. inline fxBool TypeRule::isContinuation() const    { return cont; }
  174. inline TypeResult TypeRule::getResult() const    { return result; }
  175. inline const fxStr& TypeRule::getCmd() const    { return cmd; }
  176. inline fxStr TypeRule::getErrMsg() const    { return cmd; }
  177. #endif /* _TYPERULES_ */
  178.